package com.isdust.zor.core.action;

import android.os.AsyncTask;

import com.isdust.zor.core.Habitat;
import com.isdust.zor.core.listener.onTaskCallBackListener;
import com.isdust.zor.core.util.DbUtil;
import com.isdust.zor.core.util.HttpUtil;

import java.net.URL;
import java.util.Map;

import static android.R.attr.name;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
import static com.isdust.zor.CoreConfig.URL_LOGIN;


/**
 *
 * Created by zor on 2016/10/14.
 */

public class ServerAction {
    private static HttpUtil util = HttpUtil.getInstance();

    private static AsyncTask<Void,Void,Boolean> loginTask=null;

    private static void post(final Map<String,String>post_body, final String URL, final onTaskCallBackListener<Void> onTaskCallBackListener){
        if(loginTask!=null)
            return;
        loginTask= new AsyncTask<Void,Void,Boolean>(){

            @Override
            protected Boolean doInBackground(Void ...voids) {
                try {
                    Thread.sleep(1000);
                    util.post_string(URL,post_body);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            @Override
            protected void onPostExecute(final Boolean success) {
                loginTask=null;
                if (success) {
                    onTaskCallBackListener.onSuccess();
                } else {
                   onTaskCallBackListener.onFailure("error");
                }
            }

            @Override
            protected void onCancelled() {
                loginTask=null;
                onTaskCallBackListener.onFailure("cancelled");
            }

        };
        loginTask.execute((Void)null);
    }
    public static void addHabitat(final Map<String,String>post_body, final String URL, final onTaskCallBackListener<Void> onTaskCallBackListener){
        if(loginTask!=null)
            return;
        loginTask= new AsyncTask<Void,Void,Boolean>(){

            @Override
            protected Boolean doInBackground(Void ...voids) {
                try {
                    Thread.sleep(2*1000);
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            @Override
            protected void onPostExecute(final Boolean success) {
                loginTask=null;
                if (success) {
                    onTaskCallBackListener.onSuccess();
                } else {
                    onTaskCallBackListener.onFailure("error");
                }
            }

            @Override
            protected void onCancelled() {
                loginTask=null;
                onTaskCallBackListener.onFailure("cancelled");
            }

        };
        loginTask.execute((Void)null);
    }
    public static void readHabit(final Map<String,String>post_body, final String URL, final onTaskCallBackListener<Void> onTaskCallBackListener){
        if(loginTask!=null)
            return;
        loginTask= new AsyncTask<Void,Void,Boolean>(){

            @Override
            protected Boolean doInBackground(Void ...voids) {
                try {
                    Thread.sleep(2*1000);

                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            @Override
            protected void onPostExecute(final Boolean success) {
                loginTask=null;
                if (success) {
                    onTaskCallBackListener.onSuccess((new DbUtil()).getHabits());
                } else {
                    onTaskCallBackListener.onFailure("error");
                }
            }

            @Override
            protected void onCancelled() {
                loginTask=null;
                onTaskCallBackListener.onFailure("cancelled");
            }

        };
        loginTask.execute((Void)null);
    }



}